﻿<?php
include_once("../driver/drive.php");
include_once("../driver/tableLocation.php");
class categoryWHandle
{
		private function generateInsertSql($tableName, $array)
		{
				if($tableName=='' || $array==null || !is_array($array) || count($array)<=0)
				{
					return '';
				}
        $sql = "insert into $tableName(";
        $first=0;
        foreach( $array as $key=>$value)
        {
            if($first==0)
            {
                $sql .= "$key";
                $first=1;
            }
            else
            {
                $sql .= ",$key";
            }
        }
        $sql .=")values(";
        $first=0;
        foreach( $array as $key=>$value)
        {
            if($first==0)
            {
                $sql .= "'$value'";
                $first=1;
            }
            else
            {
                $sql .= ",'$value'";
            }
        }
        $sql .= ")";
        return $sql;
		}
    private function insert_sql($array)
    {
        $tablename = tableLocation::categoryTable();
        return $this->generateInsertSql($tablename, $array);
    }
    public function insert_data($array)
    {
        $sql = $this->insert_sql($array);
        return drive::exc($sql);
    }
    public function update_category($array, $id)
    {
        $sqlk=array();
        foreach($array as $key=>$value)
        {
            array_push($sqlk," `$key`='$value' ");
        }
        $sqls = implode(",",$sqlk);
        $table = tableLocation::categoryTable();
        $sql = "update $table set $sqls where `id`='$id'";
        return drive::exc($sql);
    }
    public function delete_category($id)
    {
        $table = tableLocation::categoryTable();
        $sql="delete from $table where `id`='$id'";
        return drive::exc($sql);
        /*
        $array=array("check"=>"N");
        return $this->update_category($array,$id);
        */
    }
    public function delete_category_phy($uid)
    {
        $table = tableLocation::categoryTable();
        $sql="delete from $table where uid=$uid";
        return drive::exc($sql);
    }
    
    public function insert_category_device($array)
    {
    		$table = tableLocation::categoryDeviceTable();
    		$sql = $this->generateInsertSql($table, $array);
    		return drive::exc($sql);
    }
    public function update_category_course_booktime($categoryId, $courseId, $booktime)
    {
    		$table = tableLocation::categoryDeviceTable();
    		$sql = "update $table set `booktime`='$booktime' where `uid`='$categoryId' and `cid`='$courseId'";
    		return drive::exc($sql);
    }
    public function delete_category_device($condition)
    {
    		if($condition=='' || !is_array($condition) || count($condition)<=0)
    		{
    				return false;
    		}
    		$table = tableLocation::categoryDeviceTable();
    		$sqlk=array();
        foreach($condition as $key=>$value)
        {
            array_push($sqlk," `$key`='$value' ");
        }
        $sqls = implode(" and ",$sqlk);
        $sql = "delete from $table where $sqls";
        return drive::exc($sql);
    }
}
?>